﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLy_NhaSach
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void traCứuSáchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Tracuusach();
            frm.Show();
            frm.Activate();
        }

        private void thôngTinKháchVIPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new DanhsachVIP();
            frm.Show();
            frm.Activate();
        }

        private void thôngTinKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new DanhsachKH();
            frm.Show();
            frm.Activate();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có chắc muốn thoát không?", "Quan Ly Nha Sach", MessageBoxButtons.OKCancel);
            if (h == DialogResult.OK)
                Application.Exit();
        }

        private void sáchMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Themsach();
            frm.Show();
            frm.Activate();
        }

        private void thôngTinNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new QuanLyNV();
            frm.Show();
            frm.Activate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nhaSachDataSet.KhachHang' table. You can move, or remove it, as needed.
            //this.khachHangTableAdapter.Fill(this.nhaSachDataSet.KhachHang);
            txtmahd.Text = HeThong.Const.GetID.ToString();
            sachBindingSource.DataSource = NhaSachBUS.SachBUS.LoadSach();
           NhaSachBUS.HoaDonBUS.ThemHoaDon(txtmahd.Text, txtnhanvien.Text);
            loaiSachBindingSource.DataSource = NhaSachBUS.LoaiSachBUS.LoadLoaiSach();
            hoaDonBindingSource.DataSource = NhaSachBUS.HoaDonBUS.LoadHD(txtmahd.Text);
            khachHangBindingSource.DataSource = NhaSachBUS.KhachHangBUS.LoadKhachHang();
            nhanVienBindingSource.DataSource = NhaSachBUS.NhanVienBUS.LoadNhanVien();
            

        }

        private void thaoTácToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new QuanlyKH();
            frm.Show();
            frm.Activate();
        }

        private void thêmMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void theloaiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new Theloai();
            frm.Show();
            frm.Activate();
        }

        private void quảnLýLươngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form frm = new ThongtinNV();
            frm.Show();
            frm.Activate();
        }

        private void quảnLýKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void donDatHangBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.donDatHangBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.nhaSachDataSet);

        }

        private void donDatHangBindingNavigator_RefreshItems(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form frm = new QuanlyKH();
            frm.Show();
            frm.Activate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form frm = new QuanLyNV();
            frm.Show();
            frm.Activate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form frm = new Themsach();
            frm.Show();
            frm.Activate();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form frm = new Theloai();
            frm.Show();
            frm.Activate();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            sachBindingSource.DataSource = NhaSachBUS.SachBUS.LoadSach();
           
            
        }

        private void tracuu_Click(object sender, EventArgs e)
        {
            sachBindingSource.DataSource = NhaSachBUS.SachBUS.TheoTheLoai(maLoaiTextBox.Text);
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            //try
            sachBindingSource1.DataSource = NhaSachDAO.Sach.LoadDanhSachSachma(txtmasach.Text);
            {
                if (e.KeyCode == Keys.Enter)
                {
                    try
                    {
                        txtthanhtien.Text = (int.Parse(donGiaTextBox.Text) * int.Parse(txtsoluong.Text)).ToString();
                        NhaSachDAO.Sach.LoadDanhSachSachma(txtmasach.Text);
                        NhaSachDAO.ChiTietHoaDonDAO.ThemChiTietHoaDon(Int64.Parse(txtmasach.Text), txtmahd.Text, int.Parse(txtsoluong.Text), txtthanhtien.Text);
                        chitiethoadon.DataSource = NhaSachDAO.ChiTietHoaDonDAO.LoadChiTietHoaDonMua(txtmahd.Text);
                        txtmasach.Text = "";
                        txtsoluong.Text = "1";
                        txtmasach.Focus();
                    }
                    catch { }
                   
                }
                else
                    if (e.KeyCode == Keys.Enter)
                    {
                        txtsoluong.Focus();
                    }
            }
         // catch
            {}
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                NhaSachDAO.HoaDonDAO.CapNhatHD(txtmahd.Text);
                hoaDonBindingSource.DataSource = NhaSachDAO.HoaDonDAO.LoadHd(txtmahd.Text);
                MessageBox.Show("Số tiền khách phải trả là " + tongTienTextBox.Text + "Đồng");

                txtmahd.Text = HeThong.Const.GetID.ToString();
                sachBindingSource.DataSource = NhaSachBUS.SachBUS.LoadSach();
                NhaSachBUS.HoaDonBUS.ThemHoaDon(txtmahd.Text, txtnhanvien.Text);
                loaiSachBindingSource.DataSource = NhaSachBUS.LoaiSachBUS.LoadLoaiSach();
                hoaDonBindingSource.DataSource = NhaSachBUS.HoaDonBUS.LoadHD(txtmahd.Text);
                tongTienTextBox.Text = "0";
                donGiaTextBox.Text = "";
                txtthanhtien.Text = "";

                chitiethoadon.DataSource = null;
                
            }
            catch { }
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            sachBindingSource.DataSource = NhaSachBUS.SachBUS.LoadSach();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        

        private void tongTienTextBox_TextChanged(object sender, EventArgs e)
        {
           // MessageBox.Show(string.Format"{0}",txtthanhtien.Text);
        }

        private void txtthanhtien_TextChanged(object sender, EventArgs e)
        {

        }

        private void trợGiúpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chưa Hỗ Trợ Chức Năng Này !");
        }

        private void donGiaTextBox_TextChanged(object sender, EventArgs e)
        {

        }
        
    }
}
